﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;

namespace CodeBuilder.DevTools
{
    public partial class RegularMatchForm : UserControl
    {
        public RegularMatchForm()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 正则表达式
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnRegexMatch_Click(object sender, EventArgs e)
        {
            this.DoMatch();
        }


        private void DoMatch()
        {
            this.treeRegexResult.Nodes.Clear();
            TreeNode node = this.treeRegexResult.Nodes.Add("匹配结果：");
            string text = this.txtRegexInput.Text;
            string str2 = this.txtRegexPattern.Text;
            if (!string.IsNullOrEmpty(str2))
            {
                RegexOptions ignoreCase = RegexOptions.IgnoreCase;
                if (this.chkRegexIgnoreCase.Checked)
                {
                    ignoreCase = RegexOptions.IgnoreCase;
                }
                else if (!this.chkRegexIgnoreCase.Checked)
                {
                    ignoreCase = RegexOptions.None;
                }
                try
                {
                    MatchCollection matchs = Regex.Matches(text, str2, ignoreCase);
                    foreach (Match match in matchs)
                    {
                        TreeNode node2 = new TreeNode();
                        node2.Name = match.Groups[0].Value;
                        node2.Text = match.Groups[0].Value;
                        node2.ToolTipText = match.Groups[0].Value;
                        TreeNode node3 = node.Nodes.Add(match.Groups[0].Value);
                        for (int i = 1; i < match.Groups.Count; i++)
                        {
                            node3.Nodes.Add(string.Concat(new object[] { "Groups[", i, "]:", match.Groups[i].Value }));
                        }
                    }
                }
                catch (Exception)
                {
                }
                node.ExpandAll();
            }
        }

        private void chkRegexIgnoreCase_CheckedChanged(object sender, EventArgs e)
        {
            this.DoMatch();
        }

        private void txtRegexPattern_TextChanged(object sender, EventArgs e)
        {
            if (this.chkRegexMatch.Checked)
            {
                this.DoMatch();
            }
        }

        private void txtRegexInput_TextChanged(object sender, EventArgs e)
        {
            if (this.chkRegexMatch.Checked)
            {
                this.DoMatch();
            }
        }
    }
}
